import { request } from '@umijs/max';

/**
 * 添加数据源
 * @param params
 */
export async function addDataSource(params: DataSourceType.DataSource) {
  return request<BaseResponse<number>>('/datasource/add', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
  });
}

/**
 * 删除数据源
 * @param params
 */
export async function deleteDataSource(params: DeleteRequest) {
  return request<BaseResponse<boolean>>('/datasource/delete', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
  });
}

/**
 * 更新数据源
 * @param params
 */
export async function updateDataSource(params: DataSourceType.DataSource) {
  return request<BaseResponse<boolean>>('/datasource/update', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
  });
}

/**
 * 根据 id 获取数据源
 * @param id
 */
export async function getDataSourceById(id: number) {
  return request<BaseResponse<DataSourceType.DataSource>>(`/datasource/get`, {
    method: 'GET',
    params: { id },
  });
}

/**
 * 获取数据源列表
 * @param params
 */
export async function listDataSource(params: DataSourceType.DataSourceQueryRequest) {
  return request<BaseResponse<PageInfo<DataSourceType.DataSource>>>('/datasource/list/page', {
    method: 'GET',
    params,
  });
}

/**
 * 测试数据源连接
 * @param params
 */
export async function testDataSourceConnection(params: DataSourceType.DataSource) {
  return request<BaseResponse<boolean>>('/datasource/test', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: params,
  });
}
